package tcptestdemo1;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by L.jp
 * Description:
 * User: 86189
 * Date: 2022-08-09
 * Time: 16:33
 */
//客户端发送一个hello,收到服务器发的一个你谁啊
public class ServerDemo {
    public static void main(String[] args) throws IOException {
        //创建服务器套接字
        ServerSocket serverSocket=new ServerSocket(1000);
        
        //与客户端建立连接
        Socket accept=serverSocket.accept();
        //处理客户端发的数据
        InputStream is= accept.getInputStream();
        int  b;
        while ((b= is.read())!=-1){
            System.out.print((char) b);
        }
    
        /* OutputStream os = accept.getOutputStream();
        os.write("你谁啊?".getBytes());*/
        
        //给客户端发送数据
        //需要转换字节流为字符流然后写数据
        BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
        bufferedWriter.write("你谁啊？");
        //写入行分隔符。行分隔符字符串由系统属性line.separator定义，不一定是单个换行符 ('\n')。
        bufferedWriter.newLine();
        //将输出缓冲区刷新到底层字符流，而不刷新流本身
        bufferedWriter.flush();
        
        //关闭连接
        bufferedWriter.close();
        is.close();
        accept.close();
        serverSocket.close();
        
    }
}
